package com.egor.services.impl;

import com.egor.data.entity.Department;
import com.egor.data.repository.DepartmentRepository;
import com.egor.services.DepartmentService;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class DepartmentServiceImpl implements DepartmentService {
    private DepartmentRepository departmentRepository;

    @Override
    public List<Department> listAll(Pageable pageable) {
        return departmentRepository.findAll(pageable).getContent();
    }

    @Override
    public List<Department> listAll() {
        return departmentRepository.findAll();
    }

    @Override
    public void save(Department department) {
        if (department == null) {
            System.err.println("The department is null. Are you sure you have connected your form to the application?");
            return;
        }
        departmentRepository.save(department);
    }

    @Override
    public void delete(Department department) {
        departmentRepository.delete(department);
    }

    @Override
    public List<Department> findAll(String text, Pageable pageable) {
        return departmentRepository.search(text, pageable).getContent();
    }
}
